/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import POJO.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Master
 */
public class DAO_NhanVien {
    public List<POJO_NhanVien> LoadList() throws SQLException{
        
        DataProvider data = new DataProvider();
        data.Open();
        
        String query = "select * from nhan_vien";
        List<POJO_NhanVien> list = new ArrayList<>();
        ResultSet rs = data.ExecuteQuery(query);
        
        while(rs.next()){
            POJO_NhanVien nv = new POJO_NhanVien();
            
            nv.SetMaNV(rs.getString("MaNV"));
            nv.SetTenNV(rs.getString("HoTen"));
            nv.SetDiaChi(rs.getString("DiaChi"));
            nv.SetCMND(rs.getString("CMND"));
            nv.SetGioiTinh(rs.getString("GioiTinh"));
            nv.SetDienThoai(rs.getString("DienThoai"));
            nv.SetNgaySinh(rs.getDate("NgaySinh"));
            nv.SetNgayBD(rs.getDate("ThoiGianBatDau"));
            
            list.add(nv);
        }
        
        return list;
    }
    
    public int Delete(POJO_NhanVien nv){
        String query = String.format("delete from nhan_vien where manv = '%s'", nv.GetMaNV());
        DataProvider data = new DataProvider();
        data.Open();
        
        return data.ExeCuteUpdate(query);
    }
    
    public int Add(POJO_NhanVien nv){
        DataProvider data = new DataProvider();
        data.Open();
        
        String query = String.format("INSERT INTO `nhan_vien`(`MaNV`, `HoTen`, `CMND`, `NgaySinh`, `GioiTinh`, `DiaChi`, `DienThoai`, `ThoiGianBatDau`) VALUES ('%s','%s','%s','%s','%s','%s','%s','%s')", nv.GetMaNV(), nv.GetTenNV(), nv.GetCMND(), nv.GetNgaySinh(),nv.GetGioiTinh(), nv.GetDiaChi(), nv.GetDienThoai(), nv.GetNgayBD());
        
        return data.ExeCuteUpdate(query);
    }
    
    public int Update(POJO_NhanVien nv){
        DataProvider data = new DataProvider();
        data.Open();
        
        String query = String.format("UPDATE `nhan_vien` SET `MaNV`='%s',`HoTen`='%s',`CMND`='%s',`NgaySinh`='%s',`GioiTinh`='%s',`DiaChi`='%s',`DienThoai`='%s',`ThoiGianBatDau`='%s' WHERE MaNV = '%s'", nv.GetMaNV(), nv.GetTenNV(), nv.GetCMND(), nv.GetNgaySinh(),nv.GetGioiTinh(), nv.GetDiaChi(), nv.GetDienThoai(), nv.GetNgayBD(), nv.GetMaNV());
        
        return data.ExeCuteUpdate(query);
    }
}
